package com.entor.hqj.main;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.entor.hqj.login.LoginActivity;
import com.entor.hqj.login.R;
import com.entor.hqj.model.Item;
import com.entor.hqj.model.User;
import com.entor.hqj.util.HttpPostUtils;
import com.entor.hqj.util.ItemAdapter;
import com.entor.hqj.util.TitleBuilder;

import java.util.ArrayList;
import java.util.List;

public class ListViewActivity extends AppCompatActivity {
    private List<Item> list = new ArrayList<>();
    private Intent intent = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_list_view);
        initTitle();
        initList();

        ItemAdapter itemAdapter = new ItemAdapter(ListViewActivity.this, R.layout.item, list);
        ListView listView = (ListView) findViewById(R.id.listview);
        listView.setAdapter(itemAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Item item = list.get(position);
                User u = LoginActivity.userInfo;
                if(u.getLoginname().equals("admin")){
                    intent = new Intent(ListViewActivity.this, ItemInfoEditActivity.class);
                }else {
                    intent = new Intent(ListViewActivity.this, ItemInfoActivity.class);
                }
                intent.putExtra("id",item.getId());
                intent.putExtra("name",item.getName());
                String content = item.getContent();
                if(content==null){
                    content = "该主人很懒，没有简介内容";
                }
                intent.putExtra("content",content);
                startActivity(intent);
            }
        });

    }

    private void initTitle(){
        new TitleBuilder(this).setLeftText("返回").setMidTitleText("候选人")
                .setLeftImgOrTextListener(new leftImgOrTextListener());
    }
    class leftImgOrTextListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            finish();
        }
    }

    private void initList() {
        final String data = "";
        new Thread(){
            @Override
            public void run(){
                String response =
                        HttpPostUtils.doPostRequest("user/findUserByMobile",data);
                if(response!=null){
                    List<User> li = JSON.parseArray(response,User.class);
                    for (int i = 0; i < li.size(); i++) {
                        User user = li.get(i);
                        Item item = new Item(user.getName(), R.mipmap.umeng_socialize_gmail,user.getContent(),user.getId());
                        list.add(item);
                    }
                }
            }
        }.start();

    }
}
